package com.atguigu.algorithm;

public class FindThePeekElement {
    public static void main(String[] args) {
        int[] a=new int[]{1,3,2,1};
        System.out.println(findThePeekElement(a));
    }
    public static int findThePeekElement(int[] a){
        //处理特殊情况
        if(a==null) return -1;
        if(a.length==1)return 0;
        if(a[0]>a[1]){
            return 0;
        }
        int len=a.length;
        if(a[len-1]>a[len-2]){
            return len-1;
        }
        //此时峰值不在首尾，进行二分递归
        return findThePeek(a,1,len-1);
    }
    public static int findThePeek(int[] a,int left,int right){
        if(left>=right){
            return -1;
        }
        int mid=left+((right-left)>>1);
        if(a[mid]>a[mid-1] && a[mid] > a[mid+1]){
            return mid;
        }else if(a[mid]<a[mid-1]){
            return findThePeek(a,left,mid);
        }else{
            return findThePeek(a,mid+1,right);
        }
    }
}
